package cn.springboot.quickbi;

import cn.springboot.common.http.HttpClientUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;

/**
 * 测试quickbi
 *
 * @author junrui.wang
 * @create 2017/8/4
 */
@Controller
@ResponseBody
@RequestMapping("/quickbi")
public class QuickBiController {
    @Value("${quickbi.accessId}")
    private String accessId;
    @Value("${quickbi.accessKey}")
    private String accessKey;
    @Value("${quickbi.aliyunId}")
    private String aliyunId;
    @Value("${qucikbi.validityTime}")
    private String validityTime;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index() throws Exception {
        String url = "https://das.base.shuju.aliyun.com/api/ac3rdservice/token.json?accessId="+accessId+"&accessKey="+accessKey+"&aliyunId="+aliyunId+"&validityTime="+validityTime;
        HashMap map = HttpClientUtils.httpGetRequest(url, HashMap.class);
        /*
        "data": {

    "accessToken": {
        "accessToken": "6117b7445b773082edabcb9badaf1e38",
        "invalidTime": "2017-08-04 12:00:06",
        "registerTime": "2017-08-04 10:59:33"
    }

},
         */
        String token = ((HashMap)((HashMap)map.get("data")).get("accessToken")).get("accessToken").toString();
        String invalidTime =  ((HashMap)((HashMap)map.get("data")).get("accessToken")).get("invalidTime").toString();
        ModelMap model = new ModelMap();
        model.addAttribute("token", token);
        model.addAttribute("invalidTime", invalidTime);
        return new ModelAndView("testQuickBi", model);
    }
}
